package org.javaweb.cms_web.mapper;

import org.apache.ibatis.annotations.*;
import org.javaweb.cms_web.model.UserDTO1;

import java.util.List;

@Mapper
public interface UserMagagementMapper {

    // 查找待审核和已拒绝的用户
    @Select("SELECT user_id, nickname, name, email, application_reason, registration_time, status, audit_info FROM Users WHERE role_type IN (2, 3)")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "name", column = "name"),
            @Result(property = "email", column = "email"),
            @Result(property = "applicationReason", column = "application_reason"),
            @Result(property = "registrationTime", column = "registration_time"),
            @Result(property = "status", column = "status"),
            @Result(property = "auditInfo", column = "audit_info")
    })
    List<UserDTO1> findUsersForReview();

    // 更新用户状态（通过或拒绝）
    @Update("UPDATE Users SET status = #{status}, audit_info = #{auditInfo} WHERE user_id = #{userId}")
    int updateUserStatus(@Param("userId") Integer userId, @Param("status") int status, @Param("auditInfo") String auditInfo);

    // 根据昵称和状态查找用户（模糊匹配）
    @Select("SELECT user_id, nickname, name, email, application_reason, registration_time, status, audit_info FROM Users WHERE nickname LIKE CONCAT('%', #{nickname}, '%') AND status = #{status} AND role_type IN (2, 3)")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "nickname", column = "nickname"),
            @Result(property = "name", column = "name"),
            @Result(property = "email", column = "email"),
            @Result(property = "applicationReason", column = "application_reason"),
            @Result(property = "registrationTime", column = "registration_time"),
            @Result(property = "status", column = "status"),
            @Result(property = "auditInfo", column = "audit_info")
    })
    List<UserDTO1> findUsersByNicknameAndStatus(@Param("nickname") String nickname, @Param("status") int status);



    // 更新用户密码
    @Update("UPDATE Users SET password = #{password} WHERE user_id = #{userId}")
    int updateUserPassword(@Param("userId") Integer userId, @Param("password") String password);

    @Delete("DELETE FROM Users WHERE user_id = #{userId}")
    int deleteUserById(@Param("userId") Integer userId);
}

